const { Router } = require("express");
const Recipe = require("../model/Recipe");
const User = require("../model/User");
const Liulan = require("../model/liulan")
const router = new Router();

router.use(require('./auths.js'));

// 查询分页菜谱数据
router.get("/fyRecipe", async (req, res) => {
  // 获取参数
  const page = Number(req.query.page);
  const count = Number(req.query.count);
  // 执行更新
  const data = await Recipe.find().populate('user_id').skip(count * (page - 1))/*跳过多少条开始查询*/.limit(count)/*查询多少条数据*/;

  // 响应客户端
  res.status(200).json({ message: "ok", data });
});

// 搜索菜谱
router.get("/searchRecipe", async (req, res) => {
  const { title } = req.query
  const query = {};
  if (title !== undefined) {
    query.title = new RegExp(title);
  }
  // 执行更新
  const data = await User.find(query, { password: false })
  // 响应客户端
  res.status(200).json({ message: "ok", data });
});

// 查询总菜谱
/*url: localhost:8000/api/recipe/zRecipe
  method: get
  header: authorization (user_id：用户id)
 */
router.get("/zRecipe", async (req, res) => {
  // 获取参数


  // 执行更新
  const data = await Recipe.find();

  // 响应客户端
  res.status(200).json({ message: "ok", data });
});



// 查询所有用户
/*url: localhost:8000/api/recipe/AllUser
  method: get
  header: authorization (user_id：用户id)
 */
router.get("/AllUser", async (req, res) => {

  // 执行更新
  const data = await User.find()
  // 响应客户端
  res.status(200).json({ message: "ok", data });
});

// 查询搜索用户
/*url: localhost:8000/api/recipe/AllUser
  method: get
  header: authorization (user_id：用户id)
 */
router.get("/searchUser", async (req, res) => {
  const { user_name } = req.query
  const query = {};
  if (user_name !== undefined) {
    query.user_name = new RegExp(user_name);
  }
  // 执行更新
  const data = await User.find(query, { password: false })
  // 响应客户端
  res.status(200).json({ message: "ok", data });
});




// 首页第一个列表假数据
// url: localhost:8000/api/recipe/RecipeList1
router.get("/RecipeList1", async (req, res) => {
  // 获取参数
  let data = [
    {
      recipe_id: "6434924c32b9a9ced689a9b0"
      ,
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻食减脂谷物饭",
      issc: false,
    },
    {
      recipe_id: "6435624c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u99.png",
      title: "轻食减脂谷物饭",
      issc: false,
    },
    {
      recipe_id: "6478924c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u132.png",
      title: "轻食减脂谷物饭",
      issc: false,
    },
    {
      recipe_id: "6564924c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E6%90%9C%E7%B4%A2%E7%BB%93%E6%9E%9C/u351.png",

      issc: false,
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻食减脂谷物饭",
      issc: false,
    },

  ]


  // 响应客户端
  res.status(200).json({ message: "ok", data });
});


// 首页总菜谱假数据测试用
// url: localhost:8000/api/recipe/RecipeList
router.get("/RecipeList", async (req, res) => {
  // 获取参数
  let data = [
    {
      recipe_id: "6434924c32b9a9ced689a9b0"
      ,
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻食减脂谷物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",

    },
    {
      recipe_id: "6435624c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u99.png",
      title: "轻啊就好电话号饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6478924c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u132.png",
      title: "轻食减聚光科技看饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6564924c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E6%90%9C%E7%B4%A2%E7%BB%93%E6%9E%9C/u351.png",
      title: "轻食减狡猾狡猾的技看饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",

    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u99.png",
      title: "轻食餐的话物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u132.png",
      title: "轻食空间的空间饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u351.png",
      title: "轻卡卡卡卡卡卡卡卡物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻啦啦啦啦啦物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻卡卡中式早餐卡卡物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻啦啦啦中式早餐物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u99.png",
      title: "轻卡生日聚会卡卡物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
    {
      recipe_id: "6434424c32b9a9ced689a9b0",
      coverImg: "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u100.png",
      title: "轻啦啦生日聚会啦物饭",
      avatar:
        "https://cdn7.axureshop.com/demo/1934191/images/%E9%A6%96%E9%A1%B5/u136.png",
      user_name: "不会做饭的西瓜",
    },
  ]


  // 响应客户端
  res.status(200).json({ message: "ok", data });
});


const Collect = require("../model/Collect");
// 添加收藏
/* 
  url: localhost:8000/api/recipe/addCollect
  method: POST
  header: authorization (user_id：用户id)
  body: recipe_id: 菜谱id

*/
router.post("/addCollect", async (req, res) => {
  const user_id = req.userInfo._id;
  const { recipe_id } = req.body;
  // 添加至数据库
  await Collect.create({
    recipe_id,
    user_id
  });
  //响应客户端
  res.status(200).json({ message: "添加成功" });
})



// 取消收藏
/*url: localhost:8000/api/recipe/deleteCollect
  method: delete
  header: authorization (user_id：用户id)
  query: recipe_id  菜谱id */

router.delete("/deleteCollect", async (req, res) => {
  // 获取参数
  const { recipe_id } = req.query;
  const user_id = req.userInfo._id;

  // 执行更新
  await Collect.deleteMany({ user_id, recipe_id });

  // 响应客户端
  res.status(200).json({ message: "删除成功" });
})



// 查询一个菜谱是否有收藏   判断是否有收藏
/*url: localhost:8000/api/recipe/checkCollect
  method: get
  header: authorization (user_id：用户id)
  query: recipe_id  菜谱id
 */

router.get("/checkCollect", async (req, res) => {
  const user_id = req.userInfo._id;
  const { recipe_id } = req.query;
  const result = await Collect.findOne({ user_id, recipe_id });
  // 存在
  if (result) {
    // 响应客户端
    res.status(200).json({ isCollect: true, message: '已收藏' });
  } else {
    res.status(200).json({ isCollect: false, message: '未收藏' });
  }
})


// 查询user全部收藏
/*url: localhost:8000/api/recipe/userCollect
  method: get
  header: authorization (user_id：用户id)
 */
router.get("/userCollect", async (req, res) => {
  // 获取参数
  const user_id = req.userInfo._id;

  // 执行更新
  const data = await Collect.find({ user_id }).populate(['user_id', 'recipe_id']);     // 连表查询

  // 响应客户端
  res.status(200).json({ message: "ok", data });
});


// 添加菜谱
/* 
  url: localhost:8000/api/recipe/addRecipe
  method: POST
  header: authorization (user_id：用户id)
  body: title: 标题名称
        coverImg: 封面图片
        cookingTime:烹饪时长
        difficulty: 难度
        type1: 一级分类  几级分类不确定可能要改
        Imgtype3: 渲染到分类页面的分类图片
        material    存数组 用料 [{title:xxx,con:xxx}]
        step        存数组 步骤 [{img:xxxx,con:xxxx}]
        pushTime:发布时间
*/
router.post("/addRecipe", async (req, res) => {
  const user_id = req.userInfo._id;
  const { title, coverImg, cookingTime, difficulty, type1, Imgtype3, material, step, pushTime } = req.body;
  if (!title || !coverImg || !cookingTime || !difficulty || !type1 || !Imgtype3 || !material || !step || !pushTime) {
    res.status(400).json({ message: "缺少参数" });
    return;
  }
  // const newmaterial = typeof material === 'string' ? JSON.parse(material) : material;   //这个不知道要不要，如果报错可以试试。
  // 添加至数据库
  await Recipe.create({
    title,
    coverImg,
    cookingTime,
    difficulty,
    type1,
    Imgtype3,
    material,
    step,
    pushTime,
    user_id
  });
  //响应客户端
  res.status(200).json({ message: "添加成功" });
});



// 修改菜谱
/* 
  url: localhost:8000/api/recipe/updateRecipe
  method: patch
  header: authorization (user_id：用户id)
  body: 
        recipe_id 菜谱id
        title: 标题名称
        coverImg: 封面图片
        cookingTime:烹饪时长
        difficulty: 难度
        type1: 一级分类   几级分类不确定可能要改
        Imgtype3: 渲染到分类页面的分类图片
        material    存数组 用料 [{title:xxx,con:xxx}]
        step        存数组 步骤 [{img:xxxx,con:xxxx}]
        pushTime:发布时间
*/

router.patch("/updateRecipe", async (req, res) => {
  const user_id = req.userInfo._id;
  const { recipe_id, title, coverImg, cookingTime, difficulty, type1, Imgtype3, material, step, pushTime } = req.body;
  // 执行更新
  await Recipe.findOneAndUpdate({ user_id, _id: recipe_id }, { title, coverImg, cookingTime, difficulty, type1, Imgtype3, material, step, pushTime, });

  // 响应客户端
  res.status(200).json({ message: "更新成功" });
})


// 删除菜谱
/*url: localhost:8000/api/recipe/deleteRecipe
  method: delete
  header: authorization (user_id：用户id)
  query: recipe_id  菜谱id */

router.delete("/deleteRecipe", async (req, res) => {
  // 获取参数
  const { recipe_id } = req.query;
  const user_id = req.userInfo._id;

  // 执行更新
  await Recipe.findOneAndDelete({ user_id, _id: recipe_id });

  // 响应客户端
  res.status(200).json({ message: "删除成功" });
})



// 查询user菜谱
/*url: localhost:8000/api/recipe/userRecipe
  method: get
  header: authorization (user_id：用户id)
 */
router.get("/userRecipe", async (req, res) => {
  // 获取参数
  const user_id = req.userInfo._id;

  // 执行更新
  const data = await Recipe.find({ user_id });

  // 响应客户端
  res.status(200).json({ message: "ok", data });
});






// 以分类查询菜谱
/*url: localhost:8000/api/recipe/typeRecipe
  method: get
  header: authorization (user_id：用户id)
  query: type3  一级分类   可能要改
 */
router.get("/typeRecipe", async (req, res) => {
  // 获取参数
  const { type3 } = req.query;
  // 执行更新
  const data = await Recipe.find({ classList: type3 }).populate(['user_id']);
  // data.forEach((item, index) => {
  //   item.coverImg.forEach((items, indexs) => {
  //     data[index].coverImg[indexs] = 'http://127.0.0.1:8000' + items
  //   })
  //   item.user_id.avatar = 'http://127.0.0.1:8000' + item.user_id.avatar
  // })

  // console.log(data);
  // 响应客户端
  res.status(200).json({
    message: "ok",
    data
  });
});


// 以菜谱id查询菜谱 详情页用
/*url: localhost:8000/api/recipe/Recipe_id
  method: get
  header: authorization (user_id：用户id)
  query: recipe_id  菜谱id
 */
router.get("/Recipe_id", async (req, res) => {
  const user_id = req.userInfo._id;

  // 获取参数
  const { recipe_id } = req.query;

  // 执行更新
  const data = await Recipe.find({ _id: recipe_id });

  // 响应客户端
  res.status(200).json({ message: "ok", data, flag: user_id == data[0].user_id });
});


// 根据user_id去查user表中的user信息
router.get("/findUser", async (req, res) => {
  const { recipe_id } = req.body;
  const data = await Recipe.find({ recipe_id: recipe_id }).populate(['user_id'])
  // console.log(data);
  res.status(200).json({ message: "ok", data })
})

// 浏览记录
router.post("/liulan", async (req, res) => {
  const { user_id, liulan_time } = req.body;
  const data = await Liulan.create({ user_id: user_id, liulan_time: liulan_time })
  res.status(200).json({ message: "ok", data })
})

module.exports = router;